using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class MainMenu : MonoBehaviour
{
    private string address = "127.0.0.1";
    private int tcp_port = 27251;

    public GameObject message;
    private UI_Message ui_Message;
    public TMP_Text text_version;
    public TMP_Text text_user_info;
    public GameObject login;
    public TMP_InputField login_user;
    public TMP_InputField login_password;
    public GameObject signIn;
    public TMP_InputField signin_user;
    public TMP_InputField signin_password;
    public Button button_exit;

    GameManager gameManager;
    // Start is called before the first frame update
    void Start()
    {
        gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
        address = gameManager.server_address;
        tcp_port = gameManager.server_tcp_port;
        string version = "v" + GameObject.Find("GameManager").GetComponent<GameManager>().version;
        ui_Message = message.GetComponent<UI_Message>();
        text_version.text = version;

        UpdateUserInfo();

        login.SetActive(false);
    }

    void UpdateUserInfo()
    {
        if (gameManager.getUser() != null)
        {
            text_user_info.text = gameManager.getUser();
        }
        else
        {
            text_user_info.text = "not login in";
        }
    }

    [Obsolete]
    void Login(string user, string password)
    {
        TcpClient tcpClient = new TcpClient();
        tcpClient.Connect(address, tcp_port);

        JObject jObject = new JObject();
        jObject["action"] = "login";
        jObject["user"] = user;
        jObject["password"] = password;
        string message = jObject.ToString();
        byte[] bytes = Encoding.UTF8.GetBytes(message);

        NetworkStream stream = tcpClient.GetStream();
        stream.Write(bytes, 0, bytes.Length);
        print("Message send to Java Server:" + bytes);

        // 接收来自Java服务器的响应消息
        byte[] buffer = new byte[1024];
        int bytesRead = stream.Read(buffer, 0, buffer.Length);
        string response = Encoding.UTF8.GetString(buffer, 0, bytesRead);
        print("Received response from Java server: " + response);
        switch (response)
        {
            case "success":
                {
                    gameManager.setUser((string)jObject["user"]);
                    UpdateUserInfo();
                    ui_Message.Show("你好," + gameManager.getUser() + ".");
                    Application.LoadLevel("InitialWorld");
                    break;
                }
            case "password error":
                {
                    ui_Message.Show("密码错误");
                    break;
                }
            case "user unregistered":
                {
                    ui_Message.Show("用户未注册");
                    break;
                }
        }
    }

    void SignIn(string user, string password)
    {
        TcpClient tcpClient = new TcpClient();
        tcpClient.Connect(address, tcp_port);

        JObject jObject = new JObject();
        jObject["action"] = "signin";
        jObject["user"] = user;
        jObject["password"] = password;
        string message = jObject.ToString();
        byte[] bytes = Encoding.UTF8.GetBytes(message);

        NetworkStream stream = tcpClient.GetStream();
        stream.Write(bytes, 0, bytes.Length);
        print("Message send to Java Server:" + bytes);

        // 接收来自Java服务器的响应消息
        byte[] buffer = new byte[1024];
        int bytesRead = stream.Read(buffer, 0, buffer.Length);
        string response = Encoding.UTF8.GetString(buffer, 0, bytesRead);
        print("Received response from Java server: " + response);
        switch (response)
        {
            case "success":
                {
                    gameManager.setUser((string)jObject["user"]);
                    UpdateUserInfo();
                    ui_Message.Show("你好," + gameManager.getUser() + ".");
                    Application.LoadLevel("InitialWorld");
                    break;
                }
            case "password error":
                {
                    ui_Message.Show("密码错误");
                    break;
                }
        }
    }

    public void start_button_onclick()
    {
        print(gameManager.getUser());
        if (gameManager.getUser() == null)
        {
            print("需要登陆");
            login.SetActive(true);
        }
    }

    public void close_button_onclick()
    {
        login.SetActive(false);
        signIn.SetActive(false);
    }

    public void login_button_onclick()
    {
        print(login_user.text + " " + login_password.text);
        if (login_user.text != "" && login_password.text != "")
        {
            Login(login_user.text, login_password.text);
        }
        else
        {
            ui_Message.Show("用户名或密码不能为空");
        }
    }

    public void signin_button_onclick()
    {
        print(signin_user.text + " " + signin_user.text);
        if (signin_user.text != "" && signin_user.text != "")
        {
            SignIn(signin_user.text, signin_password.text);
        }
        else
        {
            ui_Message.Show("用户名或密码不能为空");
        }
    }

    public void go_to_signin_button_onclick()
    {
        login.SetActive(false);
        signIn.SetActive(true);
    }

    public void go_to_login_button_onclick()
    {
        signIn.SetActive(false);
        login.SetActive(true);
    }

    public void exit_button_onclick()
    {
        if (gameManager.getUser() != null)
        {
            gameManager.setUser(null);
            UpdateUserInfo();
        }
    }
}
